package com.mo.httpC;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mo.core.util.MD5Util;

public class MoHttpUtils {
	public static synchronized JSONObject callHttpGet(String url,String token){
		JSONObject json = null;
		try{
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(url);
			httpGet.setHeader("Authorization", "Bearer "+token);
			CloseableHttpResponse response1 = httpclient.execute(httpGet);
			
			try {
			    System.out.println(response1.getStatusLine());
			    HttpEntity entity1 =  response1.getEntity();
			    
			    String res = EntityUtils.toString(entity1);
			    System.out.println(res);
//			    JSON json1 = (JSON) JSON.toJSON(res);
		    	JSONObject json1 = (JSONObject) JSONObject.parse(res);//fromObject(res);
			    JSONArray arr = json1.getJSONArray("labels");
		    	/*List<VideoLabel> videoLabelList = new ArrayList<VideoLabel>();
			    System.out.println(arr.size());
			    for(int i=0;i<arr.size();i++){
			    	VideoLabel videoLabel = JSON.toJavaObject((JSON) JSON.toJSON(arr.get(i)), VideoLabel.class);
			    	videoLabelList.add(videoLabel);
//			    	System.out.println(arr.get(i));
			    }
			    System.out.println(videoLabelList.size());*/
			    //		    	
//			    json = JSONObject.fromObject(res);//=====
//			    EntityUtils.consume(entity1);//清除结果
			    return json;
			} finally {
			    response1.close();
			    httpclient.close();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static synchronized JSONObject callHttpPost(String url,String token,JSONObject jsonParam){
		JSONObject json = null;
		try{
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);
			httpPost.setHeader("Authorization", "Bearer "+token);
			StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题    
			entity.setContentEncoding("UTF-8");    
			entity.setContentType("application/json");    
			httpPost.setEntity(entity);
			System.out.println();
			CloseableHttpResponse response2 = httpclient.execute(httpPost);

			try {
			    System.out.println(response2.getStatusLine());
			    HttpEntity entity2 = response2.getEntity();
			    String res = EntityUtils.toString(entity2);
//			    json = JSONObject.fromObject(res);
//			    EntityUtils.consume(entity2);//清除结果
			    System.out.println(res);
			    return json;
			} finally {
			    response2.close();
			    httpclient.close();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/*@SuppressWarnings("deprecation")
	public static JSONObject doPost(String url,JSONObject json){
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		JSONObject response = null;
		try {
			StringEntity s = new StringEntity(json.toString());
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");//发送json数据需要设置contentType
			post.setEntity(s);
			HttpResponse res = client.execute(post);
			if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(res.getEntity());// 返回json格式：
				response = JSONObject.parseObject(result);
//				response = JSONObject.fromObject(result);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return response;
}*/
	
	static void addComment(){
		String url="http://localhost:8180/dispark/contComment/addContComment?commentType=1&contId=22&commentText=ceshi&phoneNum=13161325201";
		String key="eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21LZXkiOiJqc3doNXQiLCJzdWIiOiIxMzE2MTMyNTIwMSIsImV4cCI6MTUyODM1ODY2NCwiaWF0IjoxNTI3NzUzODY0fQ.Pt3GZTygwO0rQuebQRUCwW3Lko2icwS_Rbe_4ws4OHyx70YPenTXD-sPlLbCrZUFkhPmRtIm7Vh8pwx_OSkryw";
		JSONObject jsonParam = new JSONObject();  
		Map<String ,Object> map = new HashMap<String,Object>(); 
//		map.put("commentType", 1);
//		map.put("contId",22);
//		map.put("commentText", "这是陈小东的测试评论！");
//		map.put("phoneNum", "13161325101");
		jsonParam.put("object", map);
		//POST请求
		
//        String encrypt = MD5Util.encrypt(jsonParam.getString("object") + "n2j4gt");
//		String encrypt = MD5Util.encrypt(JSON.toJSONString(jsonParam.get("object")) + "jswh5t");
		
//        System.out.println(encrypt);
//		jsonParam.put("sign",encrypt);
		callHttpPost(url, key, jsonParam);
	}
	static void getToken(){
		String url = "http://localhost:8180/auth?userName=13161325201&password=123456";
		JSONObject jsonParam = new JSONObject();  
//		Map<String ,Object> map = new HashMap<String,Object>(); 
//		
//		map.put("userName", "13161325201");
//		map.put("password",123456);
//		jsonParam.put("object", map);
		callHttpPost(url, "", jsonParam);
		
	}
	public static void main(String[] args) {
//		addComment();
//		getToken();
//		getIntegral();
		postregistTVAPP();
	}
	static void postregistTVAPP(){
		String url="http://localhost:8280/regist/registTVAPP";
//		String url="http://localhost:8280/auth";
		String key="1eyJhbGciOiJlapYK21Goe7sqS-N7Kqusw";
		JSONObject jsonParam = new JSONObject();  
		Map<String ,Object> map = new HashMap<String,Object>(); 
		
		map.put("tvid", "APK_mo3C591EBDF821");
		map.put("channel",1);
		map.put("guid", 0);
		map.put("vendor", 5);
		map.put("model", "签到");
		jsonParam.put("object", map);
		//POST请求
		
//        String encrypt = MD5Util.encrypt(jsonParam.getString("object") + "n2j4gt");
		String encrypt = MD5Util.encrypt(JSON.toJSONString(jsonParam.get("object")) + "nzn");
		
        System.out.println(encrypt);
		jsonParam.put("sign",encrypt);
		callHttpPost(url, key, jsonParam);
	}
	static void getIntegral(){
		String url="http://localhost:8280/integral/add";
//		String url="http://localhost:8280/auth";
		String key="eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21LZXkiOiIzbnRuem4iLCJzdWIiOiJPVFRBVVRPLUNJQk41ODowMDpFMzo0Qjo3RDozNyIsImV4cCI6MTU0MjAxMDU5NywiaWF0IjoxNTQxNDA1Nzk3fQ.Ru38QTA3gD1Ket5Rsp9Jf0JGxbbLyjh83r9pMM-L9eqCAMAMhMi8Yz7bT-LCww6YlapYK21Goe7sqS-N7Kqusw";
		
		JSONObject jsonParam = new JSONObject();  
		Map<String ,Object> map = new HashMap<String,Object>(); 
		
		map.put("userName", "APK_mo3C591EBDF821");
		map.put("integralType",1);
		map.put("dealType", 0);
		map.put("amount", 5);
		map.put("desc", "签到");
		jsonParam.put("object", map);
		//POST请求
		
//        String encrypt = MD5Util.encrypt(jsonParam.getString("object") + "n2j4gt");
		String encrypt = MD5Util.encrypt(JSON.toJSONString(jsonParam.get("object")) + "3ntnzn");
		
        System.out.println(encrypt);
		jsonParam.put("sign",encrypt);
		callHttpPost(url, key, jsonParam);
		
		//GET请求
//		String urlget="http://localhost/hello/helloGet?user=admin";
//		callHttpGet(urlget, key);
	}
}
